package cn.virens.web.components.weixin.session.support;

import java.time.Duration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.virens.web.components.weixin.session.AbstractSessionManager;
import cn.virens.web.components.weixin.session.VirWxSession;
import me.chanjar.weixin.common.session.InternalSession;
import me.chanjar.weixin.common.session.InternalSessionManager;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;

public class SupportSessionManager implements WxSessionManager, InternalSessionManager {
	private final Logger logger = LoggerFactory.getLogger(SupportSessionManager.class);
	private final AbstractSessionManager sessionManager;

	private SupportSessionManager(AbstractSessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	public static SupportSessionManager of(AbstractSessionManager sessionManager) {
		return new SupportSessionManager(sessionManager);
	}

	public final AbstractSessionManager manager() {
		return sessionManager;
	}

	@Override
	public WxSession getSession(String sessionId) {
		return sessionManager.getSession(sessionId);
	}

	@Override
	public WxSession getSession(String sessionId, boolean create) {
		return sessionManager.getSession(sessionId, create);
	}

	public Duration getMaxInactiveInterval() {
		return sessionManager.getMaxInactiveInterval();
	}

	public void setMaxInactiveInterval(Duration maxInactiveInterval) {
		this.sessionManager.setMaxInactiveInterval(maxInactiveInterval);
	}

	@Override
	public InternalSession findSession(String id) {
		logger.info("Interface disable: findSession(String id)");
		return null;
	}

	@Override
	public InternalSession createSession(String sessionId) {
		logger.info("Interface disable: createSession(String sessionId)");

		return null;
	}

	@Override
	public void remove(InternalSession session) {
		if (session instanceof VirWxSession) {
			sessionManager.removeStandardSession((VirWxSession) session);
		} else {
			logger.error("Session non VirWxSession implementation class");
		}
	}

	@Override
	public void remove(InternalSession session, boolean update) {
		this.remove(session);
	}

	@Override
	public void add(InternalSession session) {
		if (session instanceof VirWxSession) {
			sessionManager.putStandardSession((VirWxSession) session);
		} else {
			logger.error("Session non VirWxSession implementation class");
		}
	}

	@Override
	public int getActiveSessions() {
		logger.info("Interface disable: getActiveSessions()");

		return 0;
	}

	@Override
	public InternalSession createEmptySession() {
		logger.info("Interface disable: createEmptySession()");

		return null;
	}

	@Override
	public InternalSession[] findSessions() {
		logger.info("Interface disable: findSessions()");

		return null;
	}

	@Override
	public void backgroundProcess() {
		logger.info("Interface disable: backgroundProcess()");
	}

	@Override
	public void setMaxInactiveInterval(int interval) {
		this.setMaxInactiveInterval(Duration.ofSeconds(interval));
	}

	@Override
	public void setProcessExpiresFrequency(int processExpiresFrequency) {
		logger.info("Interface disable: setProcessExpiresFrequency(int processExpiresFrequency)");

	}

	@Override
	public void setBackgroundProcessorDelay(int backgroundProcessorDelay) {
		logger.info("Interface disable: setBackgroundProcessorDelay(int backgroundProcessorDelay)");

	}

	@Override
	public void setMaxActiveSessions(int max) {
		logger.info("Interface disable: setMaxActiveSessions(int max)");
	}

}
